package com.kimho.notice.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.notice.domain.Notice;
import com.kimho.notice.service.NoticeService;
import com.kimho.user.domain.User;
import com.kimho.util.Util;

@Controller
@RequestMapping("/notice")
public class NoticeController {
	
	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping("/noticelist")
	public @ResponseBody Map<String, Object> getAllNotice(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> returnMap = new HashMap<String, Object>();
		PageInfo pi = new PageInfo(request);
		List<Notice> notices = this.noticeService.getAllNotice(pi);
		returnMap.put("totalCount", this.noticeService.getTotalCount());
		returnMap.put("rows", notices);
		return returnMap;
	}
	
	@RequestMapping("/save")
	public @ResponseBody Map<String, Object> saveNotice(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> returnMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String message = request.getParameter("message");
		Notice notice = new Notice();
		try {
			if(id==null||"".equals(id.trim())){
				notice.setId(Util.getUUID());
				notice.setTitle(title);
				notice.setMessage(message);
				notice.setStaff_id(((User)request.getSession().getAttribute("user")).getStaff_id());
				notice.setCreate_date(new Date());
				System.out.println(title);
				System.out.println(message.length());
				this.noticeService.saveNotice(notice);
				returnMap.put("success", true);
				returnMap.put("info", "发布公告成功");
				return returnMap;
			}else{
				notice.setId(id);
				notice.setTitle(title);
				notice.setMessage(message);
				notice.setModify_date(new Date());
				this.noticeService.updateNotice(notice);
				returnMap.put("success", true);
				returnMap.put("info", "编辑公告成功");
				return returnMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnMap.put("success", false);
			returnMap.put("info", "发布公告失败!");
			return returnMap;
		}
	}
	
	@RequestMapping("/getmessagebyid")
	public @ResponseBody Map<String, Object> getMessageById(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> returnMap = new HashMap<String, Object>();
		String noticeid = request.getParameter("id");
		if(noticeid!=null|| !("").equals(noticeid)){
			returnMap.put("rows", this.noticeService.getMessageById(noticeid));
			returnMap.put("success", true);
			return returnMap;
		}
		returnMap.put("rows", "");
		returnMap.put("success", false);
		return returnMap;
	}
	@RequestMapping("/deletenotice")
	public @ResponseBody Map<String, Object> delNotice(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> returnMap = new HashMap<String, Object>();
		try {
			String noticeid = request.getParameter("id");
			this.noticeService.delNotice(noticeid);
			returnMap.put("info", "公告删除成功!");
			returnMap.put("success", true);
			return returnMap;
		} catch (Exception e) {
			e.printStackTrace();
			returnMap.put("info", "公告删除失败!请刷新页面.");
			returnMap.put("success", false);
			return returnMap;
		}
	}
	
}
